package com.lly.bigdata.bgayx.common.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName JacksonUtils
 * @Deacription TODO
 * @Author admin
 * @Date 2021/10/28 15:17
 * @Version 1.0
 **/
public class JacksonUtils {
    private static ObjectMapper objectMapper = new ObjectMapper();

    public static <T> String obj2json(T entity) {
        try {
            return objectMapper.writeValueAsString(entity);
        } catch (JsonProcessingException var2) {
            var2.printStackTrace();
            return "{}";
        }
    }


    public static <T> T json2pojo(String json, Class<T> clz) {
        try {
            return objectMapper.readValue(json, clz);
        } catch (Exception var3) {
            var3.printStackTrace();
            return null;
        }
    }

    public static Map<String, Object> json2map(String json) {
        try {
            return (Map) objectMapper.readValue(json, Map.class);
        } catch (Exception var2) {
            var2.printStackTrace();
            return new HashMap();
        }
    }

    public static <T> Map<String, T> json2map(String json, Class<T> clz) {
        HashMap result = new HashMap();

        try {
            Map<String, Map<String, Object>> map = (Map) objectMapper.readValue(json, new TypeReference<Map<String, T>>() {
            });
            Iterator var4 = map.entrySet().iterator();

            while (var4.hasNext()) {
                Map.Entry<String, Map<String, Object>> entry = (Map.Entry) var4.next();
                result.put(entry.getKey(), map2pojo((Map) entry.getValue(), clz));
            }
        } catch (Exception var6) {
            var6.printStackTrace();
        }

        return result;
    }

    public static <T> List<T> json2list(String jsonArray, Class<T> clz) {
        try {
            JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, new Class[]{clz});
            return (List) objectMapper.readValue(jsonArray, javaType);
        } catch (Exception var3) {
            var3.printStackTrace();
            return new ArrayList();
        }
    }

    public static <T> T map2pojo(Map<String, Object> map, Class<T> clz) {
        return objectMapper.convertValue(map, clz);
    }

    public static Map<String, Object> obj2map(Object object) {
        return (Map) objectMapper.convertValue(object, Map.class);
    }


    static {
        objectMapper = new ObjectMapper();
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        objectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.getDeserializationConfig().withoutFeatures(new DeserializationFeature[]{DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES});
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
    }
}
